
import store from '@/store'

export default {
  inserted(el, binding) {
    if (!el) {
      return
    }
    const roles = store.getters && store.getters.permission_rolesBtns
    const value = binding.value
    if (value && value.length > 0) {
      const permissionRoles = value

      const hasPermission = roles.some(role => {
        return permissionRoles === role
      })

      if (!hasPermission) {
        el.parentNode && el.parentNode.removeChild(el)
      }
    } else {
      throw new Error(`need roles! Like v-permission="['admin','editor']"`)
    }
  },
  componentUpdated(el, binding) {
    if (!el) {
      return
    }
    const roles = store.getters && store.getters.permission_rolesBtns
    const value = binding.value
    if (value && value.length > 0) {
      const permissionRoles = value

      const hasPermission = roles.some(role => {
        return permissionRoles === role
      })

      if (!hasPermission) {
        el.parentNode && el.parentNode.removeChild(el)
      }
    } else {
      throw new Error(`need roles! Like v-permission="['admin','editor']"`)
    }
  }
}
